#include <rclcpp/rclcpp.hpp>
#include <rclcpp/parameter_client.hpp>

class ParameterReaderNode : public rclcpp::Node {
public:
    ParameterReaderNode() : Node("parameter_reader") {
        param_client_node_ = std::make_shared<rclcpp::Node>("param_client_node");
        // 创建参数客户端
        sync_param_client_ = std::make_shared<rclcpp::SyncParametersClient>(
            param_client_node_, "add_on_set_node");
        set_params();
    }

private:
    rclcpp::SyncParametersClient::SharedPtr sync_param_client_;
    rclcpp::Node::SharedPtr param_client_node_;
    rclcpp::TimerBase::SharedPtr timer_;
    void set_params(){
        while(!sync_param_client_->service_is_ready()) {
            RCLCPP_WARN(this->get_logger(), "Parameter service not ready, retrying...");
        }
        // 设置参数
        auto vec_result=sync_param_client_->set_parameters({rclcpp::Parameter("name", "zhangsan")});
        if(vec_result[0].successful){
            RCLCPP_INFO(this->get_logger(), "Parameter set successfully");
        }else{
            RCLCPP_INFO(this->get_logger(), "Parameter set failed");
        }
    }
};

int main(int argc, char** argv) {
    rclcpp::init(argc, argv);
    auto node = std::make_shared<ParameterReaderNode>();
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}